Skip to content

Fix URL validation and handling in URLUtil#12800

Closed
sfahaddev wants to merge 42 commits intoJabRef:mainfrom
sfahaddev:fix-for-issue-12186
Closed

Fix URL validation and handling in URLUtil#12800
sfahaddev wants to merge 42 commits intoJabRef:mainfrom
sfahaddev:fix-for-issue-12186

Conversation

@sfahaddev
Copy link

Closes #12186

Describe the changes you have made here: what, where, why, ...

  • What: Fixed URL validation and handling in the URLUtil class.
  • Where: Updated the isURL and create methods in URLUtil and added/modified tests in URLUtilTest.
  • Why: To resolve the issue where relative URLs (e.g., www.example.com) were not being handled correctly, and to ensure proper validation of URLs with various protocols (e.g., http://, https://, ftp://, file://).

Changes:

  1. isURL Method:

    • Removed unnecessary exception handling (MalformedURLException and IllegalArgumentException).
    • Now only catches URISyntaxException to correctly identify invalid URLs.
    • Ensures that only absolute URLs are considered valid.
  2. create Method:

    • Added logic to prepend http:// only to URLs without a protocol (e.g., www.example.comhttp://www.example.com).
    • Ensures that URLs with existing protocols (e.g., ftp://example.com, file:///path/to/file) are not modified.
  3. Tests:

    • Added new test cases for relative URLs, absolute URLs, and other protocols.
    • Fixed normalization issue for file URLs in tests (e.g., file:///path/to/filefile:/path/to/file).

Mandatory checks

  • I own the copyright of the code submitted and I license it under the MIT license
  • Change in CHANGELOG.md described in a way that is understandable for the average user (if change is visible to the user)
  • Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (if change is visible to the user)
  • Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

@sfahaddev sfahaddev force-pushed the fix-for-issue-12186 branch from 33a8fbf to e060945 Compare March 22, 2025 22:13
@sfahaddev sfahaddev force-pushed the fix-for-issue-12186 branch from 05de4cd to 4ea9e06 Compare March 23, 2025 22:34
@sfahaddev sfahaddev force-pushed the fix-for-issue-12186 branch from 04b92fe to eaf63e3 Compare March 23, 2025 23:16
@JabRef JabRef deleted a comment from github-actions bot Mar 24, 2025
@sfahaddev sfahaddev requested review from Siedlerchr and koppor March 24, 2025 23:28
@JabRef JabRef deleted a comment from koppor Mar 24, 2025
@sfahaddev sfahaddev requested review from InAnYan and koppor March 26, 2025 23:44
@sfahaddev sfahaddev requested a review from koppor March 30, 2025 13:36
@sfahaddev sfahaddev force-pushed the fix-for-issue-12186 branch from ac5292b to 3cee13c Compare May 1, 2025 11:36
@koppor
Copy link
Member

koppor commented May 4, 2025

Something seems to be messed up in protocol handling - especially if other protocols than http(s) are used:

> Task :jablib:test
org.jabref.logic.net.URLUtilTest

  createShouldHandleURLs(String, String)

    Test [1] www.example.com, https://www.example.com/ FAILED

    org.opentest4j.AssertionFailedError: expected: <www.example.com> but was: <https://www.example.com/>

    Test [2] example.com, https://example.com/ FAILED

    org.opentest4j.AssertionFailedError: expected: <example.com> but was: <https://example.com/>


  createShouldHandleOtherProtocols(String, String)

    Test [2] file:///path/to/file, file:/path/to/file FAILED

    org.opentest4j.AssertionFailedError: expected: <file:///path/to/file> but was: <https://file:/path/to/file>

@trag-bot
Copy link

trag-bot bot commented May 4, 2025

@trag-bot didn't find any issues in the code! ✅✨

@jabref-machine
Copy link
Collaborator

Your code currently does not meet JabRef's code guidelines. We use Checkstyle to identify issues. You can see which checks are failing by locating the box "Some checks were not successful" on the pull request page. To see the test output, locate "Tests / Checkstyle (pull_request)" and click on it.

In case of issues with the import order, double check that you activated Auto Import. You can trigger fixing imports by pressing Ctrl+Alt+O to trigger Optimize Imports.

Please carefully follow the setup guide for the codestyle. Afterwards, please run checkstyle locally and fix the issues, commit, and push.

@ParameterizedTest
@CsvSource({
// Relative URLs (should default to HTTPS)
"www.example.com, https://www.example.com",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get this - maybe it should be reversed: expected is first, input is second

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChatGPT and Co-Pilot always generate this ordering; I don't know why ^^

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @koppor, I really appreciate the learning experience and your feedback throughout this process. Since this was my first open-source contribution, I gave it my best shot but I realize I couldn’t meet the expectations fully. I’ve decided to step back and will be closing my PR. Thank you again for the opportunity and guidance.

@sfahaddev sfahaddev closed this May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

junie: IllegalArgumentException: URI is not absolute

5 participants

Comments